/**
 * related to list.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-12
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var qqHttp = require("helper/http");
var userUtil = require("url/user");
var common = require("common");
var do_SlideListView_list = ui("do_SlideListView_list");
var listdata = mm("do_ListData");
do_SlideListView_list.bindItems(listdata);
var noData = [{"template":1,"labelContent":"您还没有记录"}];
var pageNum = 1, totalPages = 1;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("result", function() {
	refreshAllData();
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
/*//右上角-创建按钮
var headerRight = ui(addheader+".headerRight");
var imgNext = ui(addheader+".imgNext");
imgNext.source="source://image/common/add1.png";
imgNext.visible = true;
headerRight.on("touch", function() {
	common.open("source://view/user/userAddress/add.ui");
});*/

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的提现记录"
})
header.refreshData();
var selectId;
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	refreshAllData();
});

/*// 设置默认地址
page.on("setDefault", function(id){
	qqHttp.post(userUtil.url.address.setDefault, {id:id}, function(data) {
		nf.toast("设置成功");
		refreshAllData();
	})
});*/

//删除地址
page.on("remove", function(id){
	qqHttp.post(userUtil.url.withdraw.remove, {id:id}, function(data) {
		nf.toast("删除成功");
		refreshAllData();
	})
});

// 刷新数据
function refreshAllData(){
	pageNum = 1;
	qqHttp.post(userUtil.url.withdraw.managePage, {page:pageNum}, function(data) {
		//deviceone.print(JSON.stringify(data));
		totalPages = data.page.totalPages;
		do_SlideListView_list.rebound();
		listdata.removeAll();
		if(data.page.content.length){
			for (var i = 0; i < data.page.content.length; i++) {
				var withdraw=data.page.content[i];
				if(withdraw.type==1){
					withdraw.typeDesc="支付宝";
				}else if(withdraw.type==2){
					withdraw.typeDesc="银行卡";
				}else if(withdraw.type==3){
					withdraw.typeDesc="微信";
				}
				withdraw.isShowBH=false;
				if (withdraw.status==1) {
					withdraw.statusDesc="申请中";
				}else if(withdraw.status==2){
					withdraw.statusDesc="提现中";
				}else if(withdraw.status==3){
					withdraw.statusDesc="已提现";
				}else if(withdraw.status==4){
					withdraw.statusDesc="已驳回";
					withdraw.isShowBH=true;
				}
			}
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		do_SlideListView_list.refreshItems();
	})
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		qqHttp.post(userUtil.url.withdraw.page, {page:pageNum}, function(data) {
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			do_SlideListView_list.rebound();
			do_SlideListView_list.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		do_SlideListView_list.rebound();
		do_SlideListView_list.refreshItems();
	}
}

//下拉列表，刷新数据
do_SlideListView_list.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//上拉列表，翻页数据
do_SlideListView_list.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});

//点击
do_SlideListView_list.on("touch", function(data){
	if (page.getData()) {
		var json = page.getData();
		// 如果传过来的参数有select，表示是选择地址的操作
		if (json.select) {
			var data_temp = listdata.getRange(0);
			app.closePage(data_temp[data]);
		}
	}
	
});